package com.yu1996.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yu1996.constant.MessageConstant;
import com.yu1996.entity.PageResult;
import com.yu1996.entity.QueryPageBean;
import com.yu1996.entity.Result;
import com.yu1996.pojo.CheckItem;
import com.yu1996.service.CheckItemService;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Description:体检检查项管理
 * @author: xing
 * @Date: 2021/6/26 22:31
 */
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {

    @Reference
    private CheckItemService checkItemService;

    /**
     * @Description 查询所有的检查项
     * @Author  xing
     * @Date   2021/6/27 22:49
     * @Return 封装的结果集
     */
    @GetMapping("/findAll")
    public Result findAll() {
        List<CheckItem> checkItemList = checkItemService.findAll();
        if (checkItemList != null && checkItemList.size() > 0) {
            Result result = new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS);
            result.setData(checkItemList);
            return result;
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }


    /**
     * @Description 添加检查项
     * @Author xing
     * @Date 2021/6/26 22:31
     * @Param 检查项
     * @Return 封装的结果集
     */
    @PostMapping("/add")
    public Result add(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.add(checkItem);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

    /**
     * @Description 分页查询
     * @Author xing
     * @Date 2021/6/26 22:31
     * @Param 分页条件
     * @Return 封装的分页结果集
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = checkItemService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;
    }


    /**
     * @Description 删除检查项
     * @Author xing
     * @Date 2021/6/27 17:26
     * @Param 检查项的id
     * @Return 封装的结果集对象
     */
    @DeleteMapping("/delete")
    public Result delete(Integer id) {
        try {
            checkItemService.delete(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }


    /**
     * @Description 根据id查询检查项
     * @Author xing
     * @Date 2021/6/27 18:33
     * @Param 检查项的id
     * @Return 封装的结果集对象
     */
    @GetMapping("/findById")
    public Result findById(Integer id) {
        try {
            CheckItem checkItem = checkItemService.findById(id);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, checkItem);
        } catch (Exception e) {
            e.printStackTrace(); //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    /**
     * @Description 修改检查项数据
     * @Author xing
     * @Date 2021/6/27 19:01
     * @Param 检查项对象
     * @Return 封装的结果集对象
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.edit(checkItem);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }
}
